package com.byit.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.byit.domain.Area;
import com.byit.enums.PromptInfo;
import com.byit.log.ChannelType;
import com.byit.log.Log;
import com.byit.log.OperatorType;
import com.byit.msg.MessageResult;
import com.byit.service.IAreaService;
import com.byit.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 地区 前端控制器
 *
 * @author tjm
 * @date 2019年1月21日
 */
@RestController
@RequestMapping("/api/sys/area")
public class AreaController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IAreaService areaService;


    /**
     * 添加
     */
    @Log(title = "地区", operate = OperatorType.DATA_INSERT, channel = ChannelType.WEB_INTER)
    @PostMapping("/add")
    public Object add(@RequestParam("name") String name,
                      @RequestParam("pid") Long pid) {
        try {
            Area area = new Area();
            area.setName(name);
            area.setPid(pid);
            areaService.insert(area);
            return MessageResult.success();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return MessageResult.error(e);
        }
    }

    /**
     * 修改
     */
    @Log(title = "地区", operate = OperatorType.DATA_UPDATE, channel = ChannelType.WEB_INTER)
    @PostMapping("/update")
    public Object update(@RequestParam("id") Long id,String name,Long pid) {
        try {
            Area area = new Area();
            if (StringUtil.isNotEmpty(name)) {
                area.setName(name);
            }
            if (StringUtil.isNotNull(pid)) {
               area.setPid(pid);
            }
            area.setId(id);
            areaService.updateById(area);
            return MessageResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return MessageResult.error(e);
        }
    }

    /**
     * 详情
     */
    @Log(title = "地区", operate = OperatorType.DATA_SELECT, channel = ChannelType.WEB_INTER)
    @PostMapping("/details")
    public Object details(@RequestParam("id") Long id) {
        Area area = areaService.selectById(id);
        return MessageResult.success(area);
    }

    /**
     * 分页列表查询
     */
    @Log(title = "地区", operate = OperatorType.DATA_SELECT, channel = ChannelType.WEB_INTER)
    @GetMapping("/list")
    public Object list(String name,Long pid) {
        EntityWrapper<Area> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("id,name,pid");
        if (StringUtil.isNotEmpty(name)) {
            wrapper.like("name", name);
        }
        if (StringUtil.isNotNull(pid)){
            wrapper.eq("pid",pid);
        }
        wrapper.orderBy("id");
        List<Area> areas = areaService.selectList(wrapper);
        return MessageResult.warpper(PromptInfo.SYS_SECCUSS, areas);
    }

    /**
     * 删除
     */
    @Log(title = "地区", operate = OperatorType.DATA_DELETE, channel = ChannelType.WEB_INTER)
    @PostMapping("/del")
    public Object delete(HttpSession session, @RequestParam("ids") String ids) {
        List<Long> idList = StringUtil.splitLong(ids, ",");
        for (Long id : idList) {
            EntityWrapper<Area> wrapper = new EntityWrapper<>();
            wrapper.setSqlSelect("id");
            wrapper.eq("pid", id);
            List<Area> areas = areaService.selectList(wrapper);
            if (areas.size() > 0){
                Area area = areaService.selectById(id);
                return MessageResult.error("《"+area.getName()+"》包含有下级地区，不能直接删除！");
            }
        }
        areaService.deleteBatchIds(idList);
        return MessageResult.success();
    }

}
